package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.UserInfo;
import com.chain.BrotherExchange.utils.Androids;

/**
 * 个人中心
 */
public class PersonalCenterViewModel extends AndroidViewModel {

    private final int uid = Androids.LocalConfig.getUid();
    private final String token = Androids.LocalConfig.getToken();

    private final MutableLiveData<Resource<String>> mModifyUserInfoLiveData;
    private MutableLiveData<Resource<UserInfo.DataBeanX.DataBean>> mUserInfoLiveData;

    public PersonalCenterViewModel(@NonNull Application application) {
        super(application);
        mUserInfoLiveData = new MutableLiveData<>();
        mModifyUserInfoLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<UserInfo.DataBeanX.DataBean>> userInfo() {
        if (!Androids.LocalConfig.isLogin()) {
            return mUserInfoLiveData;
        }

        mUserInfoLiveData.setValue(Resource.loading());
        DataRepository.get().userInfo(token, uid, result -> {
            UserInfo.DataBeanX resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                mUserInfoLiveData.setValue(Resource.success(resultData.getDataX()));
            } else if (result.isNotNetwork()) {
                mUserInfoLiveData.setValue(Resource.notNetwork());
            } else if (result.isLogonFailure()) {
                mUserInfoLiveData.setValue(Resource.tokenError(result.getMsg()));
            } else {
                mUserInfoLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mUserInfoLiveData;
    }

    public MutableLiveData<Resource<String>> modifyNickname(String nickname) {
        if (VerificationManage.verifyNickname(nickname)) {
            return modifyUserInfo(nickname, null);
        }
        return mModifyUserInfoLiveData;
    }

    public MutableLiveData<Resource<String>> modifyPortrait(ImageView portrait) {
        String base64 = Androids.toBase64(portrait);
        DataRepository.get().uploadFile(base64, result -> {
            if (result.isSucceed()) {
                modifyUserInfo(null, result.getData().getPath());
            } else {
                mModifyUserInfoLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mModifyUserInfoLiveData;
    }

    private MutableLiveData<Resource<String>> modifyUserInfo(String nickname, String path) {
        mModifyUserInfoLiveData.setValue(Resource.loading());

        DataRepository.get().modifyUserInfo(token, uid, nickname, path, call -> {
            if (call != null && call.isSucceed()) {
                mModifyUserInfoLiveData.setValue(Resource.success(call.getMsg(),call.getMsg()));
            } else {
                mModifyUserInfoLiveData.setValue(Resource.error(call.getMsg()));
            }
        });

        return mModifyUserInfoLiveData;
    }
}
